package com.sskj.wallet.ui.activity;


import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.WidgetAccess;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.AddressBean;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.ScanBean;
import com.sskj.lib.bean.WalletBean;
import com.sskj.lib.model.room.WalletBeanViewModel;
import com.sskj.lib.util.NumberUtil;
import com.sskj.lib.util.TipUtil;
import com.sskj.lib.util.WalletUtil;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.CoinAssetBean;
import com.sskj.wallet.bean.GasPriceBean;
import com.sskj.wallet.component.DaggerUserDataComponent;
import com.sskj.wallet.presenter.TransferActivityPresenter;
import com.sskj.wallet.uitl.TipWalletUtil;
import com.warkiz.widget.IndicatorSeekBar;

import java.math.BigDecimal;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = RConfig.WALLET_TRANSFER)//转账
public class TransferActivity extends BaseActivity<TransferActivityPresenter> {
    @Autowired
    String code;
    @BindView(R2.id.etAddress)
    EditText etAddress;
    @BindView(R2.id.tvAddressChoose)
    ImageView tvAddressChoose;
    @BindView(R2.id.etNum)
    EditText etNum;
    @BindView(R2.id.etMemo)
    EditText etMemo;
    @BindView(R2.id.etPhone)
    EditText etPhone;
    @BindView(R2.id.tvRecommend)
    TextView tvRecommend;
    @BindView(R2.id.tvCoinType)
    TextView tvCoinType;
    @BindView(R2.id.walletProgressbar)
    ProgressBar walletProgressbar;
    @BindView(R2.id.tvStatus)
    TextView tvStatus;
    @BindView(R2.id.tvWalletName)
    TextView tvWalletName;
    @BindView(R2.id.tvWalletMoney)
    TextView tvWalletMoney;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    @Autowired
    String address;
    @Autowired
    CoinAssetBean data;
    @BindView(R2.id.ivMinerFeeTip)
    ImageView ivMinerFeeTip;
    @BindView(R2.id.ivBlockTip)
    ImageView ivBlockTip;
    private MaterialDialog blockTip;
    private MaterialDialog minerTip;
    @Inject
    WalletBeanViewModel walletBeanViewModel;
    private WalletBean walletBeanBtc;
    private WalletBean walletBeanEth;
    private WalletBean walletBean;

    @Override
    protected int getLayoutId() {
        return R.layout.wallet_activity_transfer;
    }

    @Override
    public TransferActivityPresenter getPresenter() {
        return new TransferActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        setTitle(code + App.INSTANCE.getString(R.string.wallet_transferActivity1_j));
        RxBus.getDefault().register(this);
        DaggerUserDataComponent.create().inject(this);

        tvCoinType.setText(code);
        tvWalletName.setText(String.format(App.INSTANCE.getString(R.string.wallet_transferActivity2_j), code));
        tvWalletMoney.setText(data.getAvailable());
        setRightImage(R.mipmap.wallet_icon_scan, v -> {
            ARouter.getInstance().build(RConfig.LIB_SCAN).navigation();
        });
        AndroidNextInputs inputs = new AndroidNextInputs();
        WidgetAccess widgetAccess = new WidgetAccess(this);
        inputs
                .add(widgetAccess.findEditText(R.id.etAddress), StaticScheme.required())
                .add(widgetAccess.findEditText(R.id.etNum), StaticScheme.required())
        ;
        ClickUtil.click(btSubmit, () -> {
            if (inputs.test()) {
                if (WalletUtil.walletBean.getAddr().equals(address)) {
                    address = null;
                }
                if (TextUtils.isEmpty(tvRecommend.getText())) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.wallet_transferActivity3_j));
                    return;
                }
                if (Double.valueOf(tvRecommend.getText().toString()) == 0) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.wallet_transferActivity4_j));
                    return;
                }

                TipUtil.showPwd(this, str -> {
                    if (str[0].equals(walletBean.getPwd())) {
                        String s = new BigDecimal(tvRecommend.getText().toString()).multiply(new BigDecimal("100000")).divide(new BigDecimal(9), 8, BigDecimal.ROUND_HALF_UP).toPlainString();
                        mPresenter.transfer(etMemo.getText().toString(), WalletUtil.walletBean.getPriDe(), etAddress.getText().toString(),
                                address, etNum.getText().toString(), "90000", s);

                    } else {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.wallet_transferActivity5_j));
                    }
                });

            }
        });
        ClickUtil.click(tvAddressChoose, () -> {
            ARouter.getInstance().build(RConfig.WALLET_ADDRESS_BOOK).withBoolean(Constans.IS_SELECT, true).navigation();
        });
        ClickUtil.click(ivBlockTip, () -> {
            blockTip = TipUtil.getSureTip(this, App.INSTANCE.getString(R.string.wallet_transferActivity6_j), "", () -> {
                blockTip.dismiss();
            });
        });
        ClickUtil.click(ivMinerFeeTip, () -> {
            minerTip = TipUtil.getSureTip(this, App.INSTANCE.getString(R.string.wallet_transferActivity7_j), App.INSTANCE.getString(R.string.wallet_transferActivity8_j), () -> {
                minerTip.dismiss();

            });
        });
    }

    @Override
    protected void initData() {
        mPresenter.getGasPrice();
        walletBeanViewModel.getUsers().observe(this, walletBeans -> {
            if (walletBeans != null) {
                if (walletBeans.get(0).getCode().equals(CoinEnum.BTC.getCode())) {
                    walletBeanBtc = walletBeans.get(0);
                    walletBeanEth = walletBeans.get(1);
                } else {
                    walletBeanBtc = walletBeans.get(1);
                    walletBeanEth = walletBeans.get(0);
                }
                if (code.equals(CoinEnum.BTC.getCode())) {
                    walletBean = walletBeanBtc;
                } else {
                    walletBean = walletBeanEth;
                }
            }
        });
    }

    public void gasPrice(GasPriceBean data) {
        String recommend = new BigDecimal(data.getAverage_Gwei()).multiply(new BigDecimal("90000")).multiply(new BigDecimal("0.000000001")).toPlainString();
        tvRecommend.setHint(String.format(App.INSTANCE.getString(R.string.wallet_transferActivity9_j), NumberUtil.keepNoZero(recommend)));
        tvRecommend.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                if (TextUtils.isEmpty(tvRecommend.getText())) {
                    tvRecommend.setText(NumberUtil.keepNoZero(recommend));
                }
            }
        });
//        indicatorSeekBar.setMin(Float.valueOf(data.getAverage_Gwei()));
//        indicatorSeekBar.setMax(Float.valueOf(data.getFast_Gwei()));
//        indicatorSeekBar.setProgress(Float.valueOf(data.getAverage_Gwei()));
        if (Double.valueOf(data.getOriginBlock_time_sec()) >20) {
            walletProgressbar.setProgressDrawable(ContextCompat.getDrawable(App.INSTANCE, R.drawable.wallet_progress_red));
            tvStatus.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.wallet_red_end));
            tvStatus.setText(App.INSTANCE.getString(R.string.wallet_transferActivity10_j));
        } else if (Double.valueOf(data.getOriginBlock_time_sec()) >12) {
            walletProgressbar.setProgressDrawable(ContextCompat.getDrawable(App.INSTANCE, R.drawable.wallet_progress_yellow));
            tvStatus.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.wallet_yellow_end));
            tvStatus.setText(App.INSTANCE.getString(R.string.wallet_transferActivity11_j));
        } else {
            walletProgressbar.setProgressDrawable(ContextCompat.getDrawable(App.INSTANCE, R.drawable.wallet_progress_green));
            tvStatus.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.wallet_green_end));
            tvStatus.setText(App.INSTANCE.getString(R.string.wallet_transferActivity12_j));
        }
        ClickUtil.click(ivBlockTip, () -> {
            blockTip = TipUtil.getSureTip(this, App.INSTANCE.getString(R.string.wallet_transferActivity6_j), String.format(App.INSTANCE.getString(R.string.wallet_transferActivity13_j), data.getUnfirms(), data.getLbtxs(), NumberUtil.keepMax2(data.getBlock_time_sec())), () -> {
                blockTip.dismiss();
            });
        });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateAddress(AddressBean addressBean) {
        etAddress.setText(addressBean.getAddress());
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void scanBack(ScanBean scanBean){
        etAddress.setText(scanBean.getContent());
    }
    @Override
    protected void onDestroy() {
        RxBus.getDefault().unregister(this);
        super.onDestroy();
    }

    public void transferSuccess() {
        finish();
    }

}
